<?php

/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/15
 * Time: 9:24
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\LogModel;
use App\Models\OrderCommentModel;
use App\Models\User\UserLevelModel;
use App\Services\OrdersPayService;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;

/// App\Services\Api\MoneyBil@
class MoneyBil extends ApiBase
{


    /**
     * 交易记录
     */
    public function getMoneyJilu(Request $request)
    {
        $page = $request->post('page', 0);
        $model = MoneyBilModel::getJilu($page);
        $list = [];
        foreach ($model as $key => $vo) {
            $list[] = [
                'out_trade_no' => $vo['out_trade_no'],
                'type' => MoneyBilModel::getType($vo['type']),
                'type_code' => $vo['type'],
                'money' => $vo['money'],
                // 'bale'=>$vo['bale'],
                'notes' => $vo['notes'],
                'time' => Fun::date($vo['time']),
                'status' => MoneyBilModel::getstatus($vo['status']),
            ];
        }
        OA::Json(0, $list);
    }

    /**
     * 详情
     */
    public function getMoneyInfo(Request $request)
    {
        $out_trade_no = $request->post('out_trade_no', 0);

        if ($out_trade_no === 0) {
            OA::Json(1, '参数错误');
        }
        $uid = OA::$user->getId();
        $moneyModel = MoneyBilModel::where([
            'out_trade_no' => $out_trade_no
        ])->first();
        if (!$moneyModel) {
            OA::Json(0, '订单不存在');
        }
        if ($uid !== $moneyModel->uid) {
            OA::Json(0, '该订单不属于您的');
        }
        $data = [
            'out_trade_no' => $moneyModel->out_trade_no,
            'money' => $moneyModel->money,
            'time' => Fun::date($moneyModel->time),
            'status_str' => MoneyBilModel::getstatus($moneyModel->status),
            'status' => $moneyModel->status,
            'msg' => Fun::encode($moneyModel->msg),
            'notes' => Fun::encode($moneyModel->notes),
            'type' => $moneyModel->type,  //MoneyBilModel::type_tixian
            'type_str' => MoneyBilModel::getType($moneyModel->type),
            'zhuanzhang_uid' => ''
        ];
        if ($moneyModel->type == MoneyBilModel::type_tixian) {
            $data['tixian'] = [
                'tixian_xingming' => Fun::encode($moneyModel->tixian_xingming),
                'tixian_zhanghu' => Fun::encode($moneyModel->tixian_zhanghu)//,
                //  'msg'=>Fun::encode($moneyModel->msg)
            ];
        } elseif ($moneyModel->type == MoneyBilModel::type_zhuanchu || $moneyModel->type == MoneyBilModel::type_zhuanru) {
            $data['zhuanzhang_uid'] = \App\Models\User\UserModel::idToName($moneyModel->zhuanzhang_uid);
        }
        OA::Json(0, $data);
    }

    /**
     * 查询支付状态
     */
    public function getPaymentStatus(Request $request)
    {
        $out_trade_no = $request->post('out_trade_no', 0);

        if ($out_trade_no === 0) {
            OA::Json(1, '参数错误');
        }
        $uid = OA::$user->getId();
        $moneyModel = MoneyBilModel::where([
            'out_trade_no' => $out_trade_no
        ])->select([
             'uid',
            'money',
            'out_trade_no',
            'status',
            'play_time','play_type'
        ])->first();
        if (!$moneyModel) {
            OA::Json(1, '订单不存在');
        }
      //  var_dump($uid,$moneyModel->uid);exit;
        if ($uid !== $moneyModel->uid) {
            OA::Json(1, '该订单不属于您的');
        }
        OA::Json(0,[
           // 'money'=>OA::$user->getMoney(),
            'money'=>$moneyModel->money,
            'out_trade_no'=>$moneyModel->out_trade_no,
            'status'=>$moneyModel->status,
            'play_time'=>Fun::date($moneyModel->play_time),
            'play_type'=>$moneyModel->play_type,
            'status_str'=>MoneyBilModel::getstatus($moneyModel->status) ,
            'play_type_str'=>MoneyBilModel::getPlayTepe($moneyModel->play_type)
        ]);
    }

}
